<!DOCTYPE html>
<meta charset="utf-8">
<!-- This test requires internals because it tests Chromium's
  internal UseCounter. -->
<title>Navigation Preload use counter</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<script>
function is_navigation_preload_counted(frame) {
  // From UseCounter.h
  const ServiceWorkerNavigationPreload = 1803;

  return frame.contentWindow.internals.isUseCounted(
        frame.contentDocument, ServiceWorkerNavigationPreload);
}

promise_test(t => {
    var script = 'resources/worker.js?no-preload';
    var scope = 'resources/dummy?no-preload-passthrough';
    return service_worker_unregister_and_register(t, script, scope)
      .then(registration => {
          add_completion_callback(_ => registration.unregister());
          var worker = registration.installing;
          return wait_for_state(t, worker, 'activated');
        })
    .then(() => {
        return with_iframe(scope);
    })
      .then(frame => {
          assert_false(is_navigation_preload_counted(frame));
        });
  }, 'Navigation Preload is not counted if not enabled.');

promise_test(t => {
    var script = 'resources/worker.js';
    var scope = 'resources/dummy?respondWith';
    return service_worker_unregister_and_register(t, script, scope)
      .then(registration => {
          add_completion_callback(_ => registration.unregister());
          var worker = registration.installing;
          return wait_for_state(t, worker, 'activated');
        })
      .then(() => with_iframe(scope))
      .then(frame => {
        assert_true(is_navigation_preload_counted(frame));
        });
  }, 'Navigation Preload is use counted.');

promise_test(t => {
    var script = 'resources/worker.js';
    var scope = 'resources/dummy?passthrough';
    return service_worker_unregister_and_register(t, script, scope)
      .then(registration => {
          add_completion_callback(_ => registration.unregister());
          var worker = registration.installing;
          return wait_for_state(t, worker, 'activated');
        })
      .then(() => with_iframe(scope))
      .then(frame => {
        assert_true(is_navigation_preload_counted(frame));
        });
  }, 'Navigation Preload is use counted even if preloadResponse is not used.');
</script>
